/**
 * 
 */
package com.dhcc.cdbp.query;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 组合查询：
 * BooleanQuery本身是一个Boolean子句（clauses）的容器,这个子句可以是表示逻辑与、逻辑或、逻辑非的 一个子查询。
 * Occur.MUST:只有匹配该查询子句的文档才在考虑之列
 * Occur.SHOULD:该项可选
 * Occur.MUST_NOT:搜索的结果不会包含任务匹配改查询子句的文档
 * Occur.FILTER：不参与得分
 * @author 谢进军
 *
 */
public class BooleanQueryTest {
	private static final String fieldName1 = "content1";
	private static final String value1 = "java";
	private static final String fieldName2 = "content2";
	private static final int value2 = 5;
	private static int min = 0;  
	private static int max = 100;
	Directory directory = null;
	@Before
	public void setUp() throws Exception {
		directory = new RAMDirectory();
		IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
		IndexWriter writer = new IndexWriter(directory, config);
		Document document = new Document();
		document.add(new StringField(fieldName1, value1, Store.YES));
		document.add( new IntField(fieldName2, value2, Store.YES));
		writer.addDocument(document);
		writer.close();
	}
	@Test public void test() throws IOException {
		List<Document> result = new ArrayList<>();
		
		IndexReader reader = DirectoryReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(reader);
		
		Query query = new TermQuery(new Term(fieldName1,value1));
		Query query2 = NumericRangeQuery.newIntRange(fieldName2, min, max, true, true);
		BooleanQuery resultQuery = new BooleanQuery();
		resultQuery.add(query, Occur.MUST);
		resultQuery.add(query2,Occur.MUST);
		
		TopDocs hits = searcher.search(resultQuery, Integer.MAX_VALUE);
		assertEquals(1, hits.totalHits);
		ScoreDoc[] docs = hits.scoreDocs;
		for (ScoreDoc doc : docs) {
			int docId = doc.doc;
			Document document = searcher.doc(docId);
			result.add(document);
		}
		
		for( Document document : result){
			System.out.println(fieldName1 + ":" + document.get(fieldName1) + "	" + fieldName2 + ":" + document.get(fieldName2));
		}
		reader.close();
		directory.close();
	}
	@After public void tearDown() throws Exception {
		directory.close();
	}
}
